﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;
using System.Configuration;
using Enter.Entity;
using Enter.BizLogic;


namespace Enter.Web.SystemAdmin
{
    public class PageBase : Page
    {
        public int PageSize = 20;
        public string WebSiteName = ConfigurationManager.AppSettings["WebSiteName"];

        #region 属性


        /// <summary>
        /// 用户信息
        /// </summary>
        public Sys_OperatorEntity UserInfo
        {
            get
            {
                object ui = Session["LTR_AdminUsers"];
                if (ui != null)
                {
                    return (Sys_OperatorEntity)ui;
                }
                return null;
            }
            set
            {
                Session["LTR_AdminUsers"] = value;
            }
        }

        protected String ReturnUrl
        {
            get
            {
                return ViewState["FromUrl"].ToString();
            }
            set
            {
                ViewState["FromUrl"] = Request.UrlReferrer.AbsolutePath + "?" + Request.QueryString;
            }
        }
        public string _PageUrl = null;
        /// <summary>
        /// 登录页面地址
        /// </summary>
        public static readonly string LoginPage = System.Configuration.ConfigurationManager.AppSettings["LoginPageUrl"];
        #endregion

        protected override void OnLoad(EventArgs e)
        {
            if (UserInfo == null)
            {
                UserInfo = Sys_OperatorBLLSub.GetObjectByLoginName("admin");
            }
            try
            {
                _PageUrl = Context.Request.ServerVariables["SCRIPT_NAME"].ToString();
            }
            catch { }
            if (UserInfo == null)
            {
                GoToLoginPage("您尚未登录或登录已超时,请重新登录");
            }
            base.OnLoad(e);
        }

        #region 页面通用方法
        /// <summary>
        /// 跳到登录页
        /// </summary>
        /// <param name="Message"></param>
        protected void GoToLoginPage(string Message)
        {
            if (!string.IsNullOrEmpty(Message))
            {
                Message = string.Format("alert('{0}')", Message);
            }
            string redirect_url = Request.RawUrl;

            Regex re = new Regex(@"redirect_url=([a-zA-Z0-9_\/.&]+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            System.Text.RegularExpressions.Match m = re.Match(redirect_url);
            while (m.Success)
            {
                redirect_url = m.Groups[1].ToString();
                m = m.NextMatch();
            }
            //if (match.Success)
            //{
            //    redirect_url = match.Groups[match.Length].ToString();
            //}
            string str = string.Format("<script>top.location.href='{0}?redirect_url={1}';{2}</script>", LoginPage, Server.UrlEncode(redirect_url), Message);
            Response.Write(str);
            Response.End();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="err"></param>
        /// <param name="url"></param>
        public void throwErrorGotoURL(string err, string url)
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            builder.Append("<script language=\"javascript\">");
            builder.Append("alert(\"" + err + "\");");
            builder.Append("window.location='" + url + "';");
            builder.Append("</script>");
            Response.Write(builder.ToString());
            Response.End();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="err"></param>
        /// <param name="url"></param>
        public void throwErrorGotoURLTop(string err, string url)
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            builder.Append("<script language=\"javascript\">");
            builder.Append("alert(\"" + err + "\");");
            builder.Append("top.window.location='" + url + "';");
            builder.Append("</script>");
            Response.Write(builder.ToString());
            Response.End();
        }

        /// <summary>
        /// 转到地址页
        /// </summary>
        /// <param name="url"></param>
        public void GotoUrl(string url)
        {
            Response.Write("<SCRIPT language=\"javascript\">");
            Response.Write("window.location='" + url + "';");
            Response.Write("</SCRIPT>");
        }
        #endregion
    }
}